home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / Bag.st < prev    next >
Text File  |  2000-02-13  |  1KB  |  41 lines

  1. Class Bag :Collection
  2. ! dict count !
  3. [
  4.    new
  5.       dict <- Dictionary new
  6. |
  7.    add: newElement
  8.       dict at: newElement 
  9.           put: (1 + (dict at: newElement ifAbsent: [0]))
  10. |    
  11.    add: newObj withOccurrences: anInteger
  12.         anInteger timesRepeat: [ self add: newObj ].
  13.       ^ newObj
  14. |  
  15.    remove: oldElement ifAbsent: exceptionBlock   ! i !
  16.       i <- dict at: oldElement 
  17.           ifAbsent: [ ^ exceptionBlock value].
  18.   
  19.       (1 = i) ifTrue:  [dict removeKey: oldElement]
  20.              ifFalse:  [dict at: oldElement put: i - 1 ]
  21. |   
  22.    size
  23.       ^ dict inject: 0 into: [:x :y | x + y]
  24. |   
  25.    occurrencesOf: anElement
  26.        ^ dict at: anElement ifAbsent: [0]
  27. |
  28.    first
  29.       (count <- dict first) isNil ifTrue: [^ nil].
  30.       count  <- count - 1.
  31.       ^ dict currentKey
  32. |
  33.    next
  34.       [count notNil] whileTrue:
  35.          [ (count > 0)
  36.            ifTrue: [count <- count - 1. ^ dict currentKey]
  37.            ifFalse: [(count <- dict next) isNil
  38.                ifTrue: [^ nil] ]].
  39.       ^ nil
  40. ]
  41.